package favorGenericTypes;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * User: Georgiana.Scarlat
 * Date: 8/30/12
 * Time: 10:43 AM
 *
 */
public class ParametrizedQueue<T> {

    private T[] objects;
    private static final int INITIAL_CAPACITY = 15;
    int size;


    @SuppressWarnings("unchecked")
    public ParametrizedQueue() {
        objects = (T[]) new Object[INITIAL_CAPACITY];
    }

    public void push(T object){

        extendCapacityIfNecessary();

        objects[size++] = object;

    }

    public T pop()  {
        if(size == 0)
            throw new UnsupportedOperationException("Can't pop from empty priority queue.");
        T result = objects[--size];
        objects[size] = null;
        return result;
    }

    private void extendCapacityIfNecessary() {

        if(size == objects.length){
            objects = Arrays.copyOf(objects, size * 2);
        }
    }

    @Override
    public String toString() {
        return "LamePriorityQueue{" +
                "objects=" + (objects == null ? null : Arrays.asList(Arrays.copyOf(objects,size))) +
                '}';
    }

    public static void main(String[] args) {

        ParametrizedQueue<Integer> queue = new ParametrizedQueue<Integer>();
        queue.push(0);
        queue.push(1);
        System.out.println(queue);

        //queue.push("aa");
        queue.pop();
    }
}
